package com.dunqi.gpm.shunqing.home.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dunqi.gpm.shunqing.R;
import com.dunqi.gpm.shunqing.common.api.ApiFactory;
import com.dunqi.gpm.shunqing.common.api.RxSchedulers;
import com.dunqi.gpm.shunqing.common.base.BaseAct;
import com.dunqi.gpm.shunqing.common.base.ComObserver;
import com.dunqi.gpm.shunqing.common.base.Data;
import com.dunqi.gpm.shunqing.common.base.RecyclerAdapter;
import com.dunqi.gpm.shunqing.common.util.SwipeUtil;
import com.dunqi.gpm.shunqing.common.view.ComTitleBar;
import com.dunqi.gpm.shunqing.common.view.RecyclerItemDecoration;
import com.dunqi.gpm.shunqing.home.adapter.PlanAuditAdapter;
import com.dunqi.gpm.shunqing.home.bean.Page;
import com.dunqi.gpm.shunqing.home.bean.Project;
import com.dunqi.gpm.shunqing.home.widget.EmptyView;

import java.util.ArrayList;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;

public class PlanAuditActivity extends BaseAct implements BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.title_bar)
    ComTitleBar titleBar;
    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.swipeRefresh)
    SwipeRefreshLayout swipeRefresh;

    ArrayList<Project> data = new ArrayList<>();
    private RecyclerAdapter adapter;

    @Override
    protected void refreshData(Bundle savedInstanceState) {
        net();
    }

    private void net() {

         ApiFactory.getInstance()
                .projectAnnualPlanList(page, false, "1")
                .compose(RxSchedulers.<Data<Page<Project>>>io_main())
                .subscribe(new ComObserver<Page<Project>>() {
                    @Override
                    public void onSuccess(Page<Project> projectPage) {
                        ArrayList<Project> list = projectPage.getList();

                        data.clear();

                        if (page == 1) {
                            adapter.replaceData(list);
                        } else {
                            adapter.addData(list);
                        }

                        page++;
                        if (projectPage.isHasNextPage()) {
                            adapter.loadMoreComplete();
                        } else {
                            adapter.loadMoreEnd(true);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        adapter.loadMoreFail();
                    }

                    @Override
                    public void onFinal() {
                        swipeRefresh.setRefreshing(false);

                    }

                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposables(d);
                    }
                });
    }

    @Override
    protected void initUiAndListener() {
        SwipeUtil.init(swipeRefresh);
        recycler.setLayoutManager(new LinearLayoutManager(this));
        recycler.addItemDecoration(new RecyclerItemDecoration(1, RecyclerItemDecoration.VERTICAL));
        adapter = new PlanAuditAdapter(data);
        adapter.setEmptyView(new EmptyView(this));
        recycler.setAdapter(adapter);
        adapter.setOnItemClickListener(this);
        adapter.setOnLoadMoreListener(this, recycler);
        swipeRefresh.setOnRefreshListener(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_plan_audit;
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//        ProjectDetailActivity.start(this, data.get(position), 0);
        Project project = data.get(position);
        AuditYearPlanActivity.start(this,project);
    }

    @Override
    public void onLoadMoreRequested() {
        net();
    }

    @Override
    protected void onStart() {
        super.onStart();
        page = 1;
        net();
    }

    @Override
    public void onRefresh() {
        page = 1;
        net();
    }
}
